package com.kai.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kai.constant.StatusConstant;
import com.kai.dto.CommentDTO;
import com.kai.dto.CourierPickUpDTO;
import com.kai.dto.ExpressOrderQueryDTO;
import com.kai.entity.Comment;
import com.kai.mapper.CommentMapper;
import com.kai.mapper.CourierMapper;
import com.kai.mapper.ExpressOrderMapper;
import com.kai.result.PageResult;
import com.kai.service.CourierService;
import com.kai.vo.ExpressOrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.beans.Transient;
import java.math.BigDecimal;
import java.time.LocalDateTime;

@Service
public class CourierServiceImpl implements CourierService {

    @Autowired
    private CourierMapper courierMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private ExpressOrderMapper expressOrderMapper;

    @Override
    public PageResult queryAllOrders(ExpressOrderQueryDTO expressOrderQueryDTO) {
        //分页查询
        PageHelper.startPage(expressOrderQueryDTO.getPage(), expressOrderQueryDTO.getPageSize());

        expressOrderQueryDTO.setExpressStatus(StatusConstant.TO_BE_CONFIRMED);
        Page<ExpressOrderVO> page = courierMapper.queryAllOrders(expressOrderQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void acceptOrder(CourierPickUpDTO courierPickUpDTO) {
        LocalDateTime now = LocalDateTime.now();
        //接单时间
        courierPickUpDTO.setAcceptTime(now);
        //预计送达时间
        int hour = now.getHour();
        if (hour < 18) {
            //预计截止时间时间设置为晚上九点半
            courierPickUpDTO.setOrderDeliveryDeadline(now.withHour(21).withMinute(30).withSecond(0));
        } else {
            //预计截止时间时间设置为第二天晚上九点半
            courierPickUpDTO.setOrderDeliveryDeadline(now.plusDays(1).withHour(21).withMinute(30).withSecond(0));
        }

        courierMapper.acceptOrder(courierPickUpDTO);

    }

    @Override
    public PageResult queryAllAcceptedOrders(ExpressOrderQueryDTO expressOrderQueryDTO) {
        PageHelper.startPage(expressOrderQueryDTO.getPage(), expressOrderQueryDTO.getPageSize());
        Page<ExpressOrderVO> page = courierMapper.queryAllAcceptedOrders(expressOrderQueryDTO);

        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void pickUp(CourierPickUpDTO courierPickUpDTO) {
        courierPickUpDTO.setPickUpTime(LocalDateTime.now());
        courierMapper.pickUp(courierPickUpDTO);
    }

    @Override
    public void deliver(CourierPickUpDTO courierPickUpDTO) {
        courierPickUpDTO.setDeliveryTime(LocalDateTime.now());
        courierMapper.deliver(courierPickUpDTO);
    }

    @Override
    public Long getStatus(Long courierId) {
        return courierMapper.getStatus(courierId);
    }

    @Override
    //添加事务管理
    @Transactional(rollbackFor = {Exception.class}) //出现异常 回滚
    public void comment(CommentDTO commentDTO) {

        Comment comment = Comment.builder()
                .content(commentDTO.getContent())
                .courierId(commentDTO.getCourierId())
                .expressId(commentDTO.getExpressId())
                .build();

        //处理评价分数 分数在1-5 如果低于三分 总评分扣0.1 如果低于2扣0.3 如果低于1扣0.5
        if (commentDTO.getRating() <= 2) {
            comment.setDeductPoints(BigDecimal.valueOf(0.5));
        } else if (commentDTO.getRating() <= 3) {
            comment.setDeductPoints(BigDecimal.valueOf(0.2));
        } else if (commentDTO.getRating() <= 4) {
            comment.setDeductPoints(BigDecimal.valueOf(0.1));
        }
        if (comment.getContent() != null && !comment.getContent().isEmpty()) {
            commentMapper.addComment(comment);
        }
        if (comment.getDeductPoints() != null){
            courierMapper.comment(comment);
        }
       expressOrderMapper.evaluated(comment);

    }
}
